/**
 * 使用plus.nativeObj绘制原生的弹窗界面
 * @version 2021-10-20 zzc
 */
class CloseDialog {

	constructor() {
		this.maskLayer = {}
		this.popupEle = {}
	}

	/**
	 * 显示
	 * @param { Object } params 对象
	 * @param { Function } params.confirm 确定回调
	 * @param { String } params.icon 顶部图标地址
	 * @version 2021-10-20 zzc
	 */
	show(params) {
		this.drawView(params)
		this.maskLayer.show()
		this.popupEle.show()
	}

	/**
	 * 隐藏
	 * @version 2021-10-20 zzc
	 */
	hide() {
		this.maskLayer.hide()
		this.popupEle.hide()
	}

	/**
	 * 绘制确认对话框ui
	 * @version 2021-10-19 zzc
	 */
	drawView(params) {
		const {
			confirm,
			icon,
		} = params

		const screenWidth = plus.screen.resolutionWidth
		const screenHeight = plus.screen.resolutionHeight

		const popWidth = screenWidth * 0.8
		let popHeight = 0

		// 正文距离顶部
		let contentTop = 132

		// 弹窗容器的Padding
		const viewPadding = 20

		// 弹窗容器的宽度
		const viewWidth = parseInt(popWidth - (viewPadding * 2))
		const imageWidth = parseInt(popWidth);

		console.log(imageWidth)

		// 更新描述文字

		const textHeight = 18

		const popContent = []

		// 如果没有图标
		popContent.unshift({
			src: icon,
			id: 'logo',
			tag: 'img',
			position: {
				top: '0px',
				left: (popWidth - imageWidth) / 2 + 'px',
				width: `${imageWidth}px`,
				height: `${768 * imageWidth / 727}px`,
			}
		})

		popHeight += parseInt(768 * imageWidth / 727)

		console.log(popHeight)

		this.maskLayer = new plus.nativeObj.View('maskLayer', {
			top: '0px',
			left: '0px',
			height: '100%',
			width: '100%',
			backgroundColor: 'rgba(0,0,0,0.5)'
		})

		this.popupEle = new plus.nativeObj.View('popupEle', {
			tag: 'rect',
			top: (screenHeight - popHeight) / 2 + 'px',
			left: '10%',
			'z-index': 99999,
			height: popHeight + 'px',
			width: '80%'
		})

		// 绘制白色背景


		const confirmTextConfig = {
			tag: 'font',
			id: 'confirmText',
			text: '关闭',
			textStyles: {
				size: '14px',
				color: '#FFF',
				lineSpacing: '0%',
			},
		}

		popContent.push({
			...confirmTextConfig,
			position: {
				bottom: viewPadding + 'px',
				left: ((viewWidth - viewPadding) / 2 + viewPadding * 2) + 'px',
				width: (viewWidth - viewPadding) / 2 + 'px',
				height: '40px',
			}
		});

		this.popupEle.draw(popContent);

		this.popupEle.addEventListener('click', (e) => {
			confirm && confirm()
			this.hide()
		})
	}
}

export default new CloseDialog()